package Leetcode第二期;

import java.util.Arrays;

/**
 * @author : K k
 * @date : 15:20 2020/9/19
 */
public class 不同路径_62 {
    int count=0;
    boolean visited;

    //思路1：深度优先 超时（不推荐）
    public int uniquePaths(int m, int n) {
        dfsSolver(m,n,0,0);
        return count;
    }

    private void dfsSolver(int m, int n, int i, int j) {
        //终止条件
        if (i==m-1&& j==n-1){
            count++;
            return;
        }
        if(isArea(m,n,i+1,j)){
            dfsSolver(m,n,i+1,j);
        }
        if(isArea(m,n,i,j+1)){
            dfsSolver(m,n,i,j+1);
        }
    }

    private boolean isArea(int m, int n, int i, int j) {
        return i<n&&i>=0 && j<m &&j>=0;
    }

    //动态规划: 每总走法的路径长不变为m+n，排列组合C m-1 m+n-2
    //杨辉三角：dp[i][j]=dp[i-1][j]+dp[i][j-1]
    public int uniquePathsA(int m, int n) {
        int[][] dp = new int[m][n];
        for (int i = 0; i < n; i++) {dp[0][i] = 1;}
        for (int i = 0; i < m; i++) {dp[i][0] = 1;}
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];

    }


}
